/**
 * Created by TRwin7SP1 on 2016/8/15.
 */
angular.module('inspinia')
  .controller('registerCtrl', function ($scope, $timeout, $state, toaster, fileReader, _undefined, myToaster, values, users,throttleFn) {
    init();
    function init() {
      $scope.user = {};


      $scope.payments = values.payments;

      $scope.treatedArea = values.treatedArea;

      $scope.marryStatus = values.marryStatus;

      $scope.user.gender = 0;
      $scope.user.status = 1;
      $scope.chooseImage = true;
      $scope.chooseImage2 = true;
      $scope.imageChange={
        "height":$("#headimage").width(),
        "width":$("#headimage").width()
      }
      var citySelector;
      $(function () {
        citySelector = function () {
          var province = $("#userdetail-province");
          var city = $("#userdetail-city");
          var district = $("#userdetail-area");
          var preProvince = $("#pre_province");
          var preCity = $("#pre_city");
          var preDistrict = $("#pre_district");
          var jsonProvince = "plugins/content/json-array-of-province.js";
          var jsonCity = "plugins/content/json-array-of-city.js";
          var jsonDistrict = "plugins/content/json-array-of-district.js";
          var hasDistrict = true;
          var initProvince = "<option value='0'>省份</option>";
          var initCity = "<option value='0'>城市</option>";
          var initDistrict = "<option value='0'>区县</option>";
          return {
            Init: function () {
              var that = this;
              that._LoadOptions(jsonProvince, preProvince, province, null, 0, initProvince);
              province.change(function () {
                that._LoadOptions(jsonCity, preCity, city, province, 2, initCity);
              });
              if (hasDistrict) {
                city.change(function () {
                  that._LoadOptions(jsonDistrict, preDistrict, district, city, 4, initDistrict);
                });
                province.change(function () {
                  city.change();
                });
              }
              province.change();
            },
            _LoadOptions: function (datapath, preobj, targetobj, parentobj, comparelen, initoption) {
              $.get(datapath,
                function (r) {
                  var t = ''; // t: html容器
                  var s; // s: 选中标识
                  var pre; // pre: 初始值
                  if (preobj == undefined) {
                    pre = 0;
                  } else {
                    pre = preobj.val();
                  }
                  for (var i = 0; i < r.length; i++) {
                    s = '';
                    if (comparelen === 0) {
                      if (pre !== "" && pre !== 0 && r[i].code === pre) {
                        s = ' selected=\"selected\" ';
                        pre = '';
                      }
                      t += '<option value=' + r[i].code +'#'+ r[i].name+ s + '>' + r[i].name + '</option>';
                    } else {
                      var p = parentobj.val();
                      if (p.substring(0, comparelen) === r[i].code.substring(0, comparelen)) {
                        if (pre !== "" && pre !== 0 && r[i].code === pre) {
                          s = ' selected=\"selected\" ';
                          pre = '';
                        }
                        t += '<option value=' + r[i].code +'#'+ r[i].name+ s + '>' + r[i].name + '</option>';
                      }
                    }
                  }
                  if (initoption !== '') {
                    targetobj.html(initoption + t);
                  } else {
                    targetobj.html(t);
                  }
                  targetobj.selectpicker('refresh');
                }, "json");
            }
          };
        }();
        citySelector.Init();
      });
    }

    //注册
    $scope.register = function () {
      $scope.submit = true;

      var _regAry = localStorage.getItem('regObj');
      if (_regAry) {
        var registerList = JSON.parse(localStorage.getItem('regObj'));
        registerList.push($scope.user);
        localStorage.setItem('regObj', JSON.stringify(registerList));
      } else {
        var registerList = new Array();
        registerList.push($scope.user);
        localStorage.setItem('regObj', JSON.stringify(registerList));
        // console.log("firstStorge:"+localStorage.getItem('registerObj'));
      }
      toaster.pop({
        type: 'success',
        title: '注册成功',
        showCloseButton: true,
        timeout: 2200
      })
      setTimeout(function () {
        $state.go('user.userList');
      }, 500)
    }
    //图像上传
    $scope.getFile = function () {
      fileReader.readAsDataUrl($scope.file, $scope)
        .then(function (result) {
          $scope.imageSrc = result;
        });
    };
    $scope.getFile2 = function () {
      fileReader.readAsDataUrl($scope.file, $scope)
        .then(function (result) {
          $scope.imageSrc1 = result;
        });
    };
    $scope.openFile = function () {
      angular.element("#myfile").click();
    };
    $scope.printImage = function () {
      angular.element("#mycode").click();
    };
    $scope.imageShow = function () {
      // angular.element("#replaceimage").hide();
      $scope.chooseImage = false;
    };
    $scope.imageShow2 = function () {
      $scope.chooseImage2 = false;
    }
    //病案号查询
    $scope.searchCase = function () {
      if ($scope.user.caseNum != "123") {
        myToaster('warning', '', '请输入正确病案号');
        $scope.user = {};
      }
      else {
        $scope.casenum = $scope.user.caseNum;
        myToaster('success', '', '查询成功');
        $scope.user = values.userMessage;
        $scope.user.caseNum = $scope.casenum;
      }
    }
    //表单预览
    $scope.openForm = function () {
      $scope.user.birthday = $("#userdatabirthday").val();
      $scope.user.visitHosipital = $("#visitHosipital").val();

      localStorage.setItem('registerForm', JSON.stringify($scope.user));
      $state.go('user.hospitalizedCard');
    }

    window.onresize=throttleFn(function () {
      $scope.imageChange={
            "width":($("#headimage").width())+"px",
            "height":($("#headimage").width())+"px",
          }
    },100);

  });
